﻿ 
var serverUrl="http://127.0.0.1:12138"
var server_token=""
  var xmlHttpReg = null;

  if (window.ActiveXObject) {//如果是IE
      xmlHttpReg = new ActiveXObject("Microsoft.XMLHTTP");
  } else if (window.XMLHttpRequest) {
      xmlHttpReg = new XMLHttpRequest(); //实例化一个xmlHttpReg
  }
this.requestArray=[];
var xmlHttpRegWorking=false;



var doResult1 = function () {
		if(xmlHttpReg.readyState != 4){
			return ;
		}
	  xmlHttpRegWorking=false;
    if (xmlHttpReg.status == 200) {//4代表执行完成
	 
	      try{
	      	var res= JSON.parse(xmlHttpReg.responseText)
	      	requestArray[requestArray.length-1].fun(res)
	      }catch(e){
	      	console.error(e);
      	}
    }
    if(xmlHttpReg.status == 0||xmlHttpReg.status == 404 ){
    		
    	if(requestArray[requestArray.length-1].errFun){
	    			requestArray[requestArray.length-1].errFun(res)
    	}
 			
    }
    
    
		setTimeout(ajax, 2000/requestArray.length);
}
var ajax= function(){
		if(requestArray.length==0||xmlHttpRegWorking){
					setTimeout(ajax, 1000);
					return ;
		}
		
		var request=requestArray[0];
		this.requestArray.shift();
		this.requestArray.push(request);
		if(request.stop){
			return ;
		}
	 	
	 	if(!request.ingore&&(!request.perFun||request.perFun())){
	 		 var url=request.url;
	 		 if(url.substring(0,4)!="http"){
	 		 	url=serverUrl+url
	 		 	}
	 		 	xmlHttpRegWorking=true;
	 			xmlHttpReg.open("get", url, true);
	 			xmlHttpReg.setRequestHeader("token",server_token); 
				try{
				  xmlHttpReg.onreadystatechange = doResult1; //设置回调函数
					xmlHttpReg.send();
				}catch(e){
					alert("发送异常")
				}
 		}else{
 				ajax();
 			
 		}
 	
	
}
ajax();

function clickAjax(url,fun){
				if(xmlHttpRegWorking){
					return;
				}
				xmlHttpRegWorking=true;
		    xmlHttpReg.open("get", serverUrl+url, true);
			   xmlHttpReg.setRequestHeader("token",server_token); 
			  xmlHttpReg.onreadystatechange =function () {
								if(xmlHttpReg.readyState != 4){
									return ;
								}
										xmlHttpRegWorking=false;
						    if (xmlHttpReg.status == 200) {
						      	var res= JSON.parse(xmlHttpReg.responseText)
						      	fun(res)
						      
						    }
							
						}
				xmlHttpReg.send();
				
}


	









